package zry.Operate;

import zry.Search.*;
import zry.Achievement.*;
import zry.Achievement.Other.*;
import zry.Achievement.Patent.*;
import zry.Achievement.Platform.*;
import zry.Achievement.Report.*;
import zry.Achievement.Reward.*;
import zry.Achievement.Standard.*;
import zry.Achievement.Textbook.*;
import zry.Achievement.Thesis.*;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Update {
    public void updateApplyAchievement(String userid) {
        AchievementDAO achievementDAO = new AchievementDAOImpl();
        SearchCriteriaAchievement searchCriteriaAchievement = new SearchCriteriaAchievement();
        searchCriteriaAchievement.setS_id(userid);
        List<Achievement> achievements = achievementDAO.findAchievements(searchCriteriaAchievement);

        //去掉已经在审核中的申请
        achievements.removeIf(a -> !a.getP_review().equals(0));

        System.out.println("成果id\t\t成果类型\t\t用户id\t\t初审结果\t\t终审结果");
        System.out.println(" " + achievements.toString().replaceAll("\\[", "").replaceAll(",", "").replaceAll("]", ""));

        //将本人的成果认定申请id存入一个list，用户只能修改本人的未经过审核的申请
        List<String> achievementIds = new ArrayList<>();
        for (Achievement a : achievements) {
            achievementIds.add(a.getA_id());
        }

        System.out.println("——————————————————————\n" +
                "请输入要修改的成果id");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String input = scanner.nextLine();
            //只有输入的成果id在本人的list中，才能正确显示
            if (achievementIds.contains(input)) {
                Achievement achievement = achievementDAO.getAchievement(input);
                System.out.println("成果id\t\t成果类型\t\t用户id\t\t初审结果\t\t终审结果");
                System.out.println(achievement);
                switch (achievement.getA_type()) {
                    case "论文":
                        ThesisDAO thesisDAO = new ThesisDAOImpl();
                        Thesis thesis = thesisDAO.getThesis(input);
                        System.out.println(thesis);
                        updateApplyThesis(thesis);
                        break;
                    case "教材":
                        TextbookDAO textbookDAO = new TextbookDAOImpl();
                        Textbook textbook = textbookDAO.getTextbook(input);
                        System.out.println(textbook);
                        updateApplyTextbook(textbook);
                        break;
                    case "标准":
                        StandardDAO standardDAO = new StandardDAOImpl();
                        Standard standard = standardDAO.getStandard(input);
                        System.out.println(standard);
                        updateApplyStandard(standard);
                        break;
                    case "奖励":
                        RewardDAO rewardDAO = new RewardDAOImpl();
                        Reward reward = rewardDAO.getReward(input);
                        System.out.println(reward);
                        updateApplyReward(reward);
                        break;
                    case "报告":
                        ReportDAO reportDAO = new ReportDAOImpl();
                        Report report = reportDAO.getReport(input);
                        System.out.println(report);
                        updateApplyReport(report);
                        break;
                    case "平台":
                        PlatformDAO platformDAO = new PlatformDAOImpl();
                        Platform platform = platformDAO.getPlatform(input);
                        System.out.println(platform);
                        updateApplyPlatform(platform);
                        break;
                    case "专利":
                        PatentDAO patentDAO = new PatentDAOImpl();
                        Patent patent = patentDAO.getPatent(input);
                        System.out.println(patent);
                        updateApplyPatent(patent);
                        break;
                    case "其他":
                        OtherDAO otherDAO = new OtherDAOImpl();
                        Other other = otherDAO.getOther(input);
                        System.out.println(other);
                        updateApplyOther(other);
                        break;
                }
                break;
            } else {
                System.out.println("成果id输入错误，请输入本人的成果id");
            }
        }
    }

    //修改论文的成果认定申请
    public void updateApplyThesis(Thesis thesis) {
        System.out.println("————————开始修改———————");
        ThesisDAO thesisDAO = new ThesisDAOImpl();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入论文名称");
        thesis.setT_name(scanner.nextLine());
        System.out.println("请输入刊物名称");
        thesis.setJ_name(scanner.nextLine());
        while (true) {
            System.out.println("请输入论文状态（1.录用未发表 2.已发表）");
            String input = scanner.nextLine();
            if (input.equals("1")) {
                thesis.setT_type("录用未发表");
                break;
            } else if (input.equals("2")) {
                thesis.setT_type("已发表");
                System.out.println("发布时间");
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date date = new Date(simpleDateFormat.parse(scanner.nextLine()).getTime());
                    thesis.setT_time(date);
                    break;
                } catch (Exception e) {
                    System.out.println("时间格式错误");
                }
            } else {
                System.out.println("请输入1-2的数字");
            }
        }
        System.out.println("请输入索引类型");
        thesis.setI_type(scanner.nextLine());
        System.out.println("请输入论文归属库");
        thesis.setL_type(scanner.nextLine());
        System.out.println("请输入佐证材料");
        thesis.setT_supporting(scanner.nextLine());
        System.out.println("是否确认修改（y/n）");
        if (scanner.nextLine().equals("y")) {
            if (thesisDAO.updateThesis(thesis)) {
                System.out.println("修改提交成功");
            } else {
                System.out.println("修改有误，提交失败");
            }
        }
    }

    //修改奖励的成果认定申请
    public void updateApplyReward(Reward reward) {
        System.out.println("————————开始修改———————");
        RewardDAO rewardDAO = new RewardDAOImpl();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入奖励名称");
        reward.setR_name(scanner.nextLine());
        System.out.println("请输入奖励等级（国家级/省级···）");
        reward.setR_type(scanner.nextLine());
        System.out.println("请输入获奖等级（特等奖/一等奖/二等奖···）");
        reward.setR_level(scanner.nextLine());
        System.out.println("请输入排名（可以不输入，直接按回车键）");
        reward.setR_ranking(scanner.nextLine());
        while (true) {
            System.out.println("请输入获奖时间");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = new Date(simpleDateFormat.parse(scanner.nextLine()).getTime());
                reward.setR_time(date);
                break;
            } catch (Exception e) {
                System.out.println("时间格式错误");
            }
        }
        System.out.println("请输入佐证材料");
        reward.setR_supporting(scanner.nextLine());
        System.out.println("是否确认修改（y/n）");
        if (scanner.nextLine().equals("y")) {
            if (rewardDAO.updateReward(reward)) {
                System.out.println("修改提交成功");
            } else {
                System.out.println("修改有误，提交失败");
            }
        }
    }

    //修改标准的成果认定申请
    public void updateApplyStandard(Standard standard) {
        System.out.println("————————开始修改———————");
        StandardDAO standardDAO = new StandardDAOImpl();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入标准名称");
        standard.setS_name(scanner.nextLine());
        System.out.println("请输入标准等级（国家标准/省部级地方标准/行业标准/团队标准···）");
        standard.setS_type(scanner.nextLine());
        while (true) {
            System.out.println("请输入标准发布时间");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = new Date(simpleDateFormat.parse(scanner.nextLine()).getTime());
                standard.setS_time(date);
                break;
            } catch (Exception e) {
                System.out.println("时间格式错误");
            }
        }
        System.out.println("请输入佐证材料");
        standard.setS_supporting(scanner.nextLine());
        System.out.println("是否确认修改（y/n）");
        if (scanner.nextLine().equals("y")) {
            if (standardDAO.updateStandard(standard)) {
                System.out.println("修改提交成功");
            } else {
                System.out.println("修改有误，提交失败");
            }
        }
    }

    //修改其他成果的成果认定申请
    public void updateApplyOther(Other other) {
        System.out.println("————————开始修改———————");
        OtherDAO otherDAO = new OtherDAOImpl();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入其他成果名称");
        other.setO_name(scanner.nextLine());
        System.out.println("请输入内容");
        other.setO_content(scanner.nextLine());
        System.out.println("请输入佐证材料");
        other.setO_supporting(scanner.nextLine());
        System.out.println("是否确认修改（y/n）");
        if (scanner.nextLine().equals("y")) {
            if (otherDAO.updateOther(other)) {
                System.out.println("修改提交成功");
            } else {
                System.out.println("修改有误，提交失败");
            }
        }
    }

    //修改教材的成果认定申请
    public void updateApplyTextbook(Textbook textbook) {
        System.out.println("————————开始修改———————");
        TextbookDAO textbookDAO = new TextbookDAOImpl();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入教材名称");
        textbook.setT_name(scanner.nextLine());
        System.out.println("请输入教材出版社");
        textbook.setT_publisher(scanner.nextLine());
        while (true) {
            System.out.println("请输入教材出版时间");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = new Date(simpleDateFormat.parse(scanner.nextLine()).getTime());
                textbook.setT_time(date);
                break;
            } catch (Exception e) {
                System.out.println("时间格式错误");
            }
        }
        System.out.println("请输入贡献度");
        textbook.setT_contribution(scanner.nextLine());
        System.out.println("请输入佐证材料");
        textbook.setT_supporting(scanner.nextLine());
        System.out.println("是否确认修改（y/n）");
        if (scanner.nextLine().equals("y")) {
            if (textbookDAO.updateTextbook(textbook)) {
                System.out.println("修改提交成功");
            } else {
                System.out.println("修改有误，提交失败");
            }
        }
    }

    //修改专利的成果认定申请
    public void updateApplyPatent(Patent patent) {
        System.out.println("————————开始修改———————");
        PatentDAO patentDAO = new PatentDAOImpl();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入专利名称");
        patent.setP_name(scanner.nextLine());
        System.out.println("请输入专利类型");
        patent.setP_type(scanner.nextLine());
        System.out.println("请输入专利号");
        patent.setP_number(scanner.nextLine());
        while (true) {
            System.out.println("请输入专利发布时间");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = new Date(simpleDateFormat.parse(scanner.nextLine()).getTime());
                patent.setP_time(date);
                break;
            } catch (Exception e) {
                System.out.println("时间格式错误");
            }
        }
        System.out.println("请输入专利状态");
        patent.setP_status(scanner.nextLine());
        System.out.println("请输入贡献度");
        patent.setP_contribution(scanner.nextLine());
        System.out.println("请输入佐证材料");
        patent.setP_supporting(scanner.nextLine());
        System.out.println("是否确认修改（y/n）");
        if (scanner.nextLine().equals("y")) {
            if (patentDAO.updatePatent(patent)) {
                System.out.println("修改提交成功");
            } else {
                System.out.println("修改有误，提交失败");
            }
        }
    }

    //修改报告的成果认定申请
    public void updateApplyReport(Report report) {
        System.out.println("————————开始修改———————");
        ReportDAO reportDAO = new ReportDAOImpl();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入报告名称");
        report.setR_name(scanner.nextLine());
        System.out.println("请输入报告类型");
        report.setR_type(scanner.nextLine());
        System.out.println("请输入报告服务单位");
        report.setR_dept(scanner.nextLine());
        while (true) {
            System.out.println("请输入报告时间");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = new Date(simpleDateFormat.parse(scanner.nextLine()).getTime());
                report.setR_time(date);
                break;
            } catch (Exception e) {
                System.out.println("时间格式错误");
            }
        }
        System.out.println("请输入贡献度");
        report.setR_contribution(scanner.nextLine());
        System.out.println("请输入佐证材料");
        report.setR_supporting(scanner.nextLine());
        System.out.println("是否确认修改（y/n）");
        if (scanner.nextLine().equals("y")) {
            if (reportDAO.updateReport(report)) {
                System.out.println("修改提交成功");
            } else {
                System.out.println("修改有误，提交失败");
            }
        }
    }

    //修改软硬件平台开发证明的成果认定申请
    public void updateApplyPlatform(Platform platform) {
        System.out.println("————————开始修改———————");
        PlatformDAO platformDAO = new PlatformDAOImpl();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入平台名称");
        platform.setP_name(scanner.nextLine());
        System.out.println("请输入平台服务单位");
        platform.setP_dept(scanner.nextLine());
        while (true) {
            System.out.println("请输入平台上线时间");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = new Date(simpleDateFormat.parse(scanner.nextLine()).getTime());
                platform.setP_time(date);
                break;
            } catch (Exception e) {
                System.out.println("时间格式错误");
            }
        }
        System.out.println("请输入贡献度");
        platform.setP_contribution(scanner.nextLine());
        System.out.println("请输入佐证材料");
        platform.setP_supporting(scanner.nextLine());
        System.out.println("是否确认修改（y/n）");
        if (scanner.nextLine().equals("y")) {
            if (platformDAO.updatePlatform(platform)) {
                System.out.println("修改提交成功");
            } else {
                System.out.println("修改有误，提交失败");
            }
        }
    }
}